<?php

namespace app\admin\model\express;

use think\Model;


class Coupon extends Model
{

    

    

    // 表名
    protected $name = 'coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'is_used_text',
        'is_expiry_text',
        'expiry_time_text'
    ];
    

    
    public function getTypeList()
    {
        return ['new' => __('Type new'), 'share' => __('Type share'), 'be_share' => __('Type be_share')];
    }

    public function getIsUsedList()
    {
        return ['Y' => __('Is_used y'), 'N' => __('Is_used n')];
    }

    public function getIsExpiryList()
    {
        return ['Y' => __('Is_expiry y'), 'N' => __('Is_expiry n')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsUsedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_used']) ? $data['is_used'] : '');
        $list = $this->getIsUsedList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsExpiryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_expiry']) ? $data['is_expiry'] : '');
        $list = $this->getIsExpiryList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExpiryTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expiry_time']) ? $data['expiry_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setExpiryTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
